//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class ProductReviewDBManager
    {
        #region Methods
        public static ProductReview GetByProductReviewID(int ProductReviewID)
        {
            ProductReview productReview = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductReviewLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "ProductReviewID", DbType.Int32, ProductReviewID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    productReview = new ProductReview();
                    productReview.ProductReviewID = NopDataHelper.GetInt(dataReader, "ProductReviewID");
                    productReview.ProductID = NopDataHelper.GetInt(dataReader, "ProductID");
                    productReview.CustomerID = NopDataHelper.GetInt(dataReader, "CustomerID");
                    productReview.Title = NopDataHelper.GetString(dataReader, "Title");
                    productReview.ReviewText = NopDataHelper.GetString(dataReader, "ReviewText");
                    productReview.Rating = NopDataHelper.GetInt(dataReader, "Rating");
                    productReview.HelpfulYesTotal = NopDataHelper.GetInt(dataReader, "HelpfulYesTotal");
                    productReview.HelpfulNoTotal = NopDataHelper.GetInt(dataReader, "HelpfulNoTotal");
                    productReview.IsApproved = NopDataHelper.GetBoolean(dataReader, "IsApproved");
                    productReview.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                }
            }
            return productReview;
        }

        public static ProductReviewCollection GetByProductID(int ProductID, bool showHidden)
        {
            ProductReviewCollection productReviewCollection = new ProductReviewCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductReviewLoadByProductID");
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, ProductID);
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    ProductReview productReview = new ProductReview();
                    productReview.ProductReviewID = NopDataHelper.GetInt(dataReader, "ProductReviewID");
                    productReview.ProductID = NopDataHelper.GetInt(dataReader, "ProductID");
                    productReview.CustomerID = NopDataHelper.GetInt(dataReader, "CustomerID");
                    productReview.Title = NopDataHelper.GetString(dataReader, "Title");
                    productReview.ReviewText = NopDataHelper.GetString(dataReader, "ReviewText");
                    productReview.Rating = NopDataHelper.GetInt(dataReader, "Rating");
                    productReview.HelpfulYesTotal = NopDataHelper.GetInt(dataReader, "HelpfulYesTotal");
                    productReview.HelpfulNoTotal = NopDataHelper.GetInt(dataReader, "HelpfulNoTotal");
                    productReview.IsApproved = NopDataHelper.GetBoolean(dataReader, "IsApproved");
                    productReview.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    productReviewCollection.Add(productReview);
                }
            }
            return productReviewCollection;
        }

        public static void DeleteProductReview(int ProductReviewID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductReviewDelete");
            db.AddInParameter(dbCommand, "ProductReviewID", DbType.Int32, ProductReviewID);
            int retValue = db.ExecuteNonQuery(dbCommand);
        }

        public static ProductReviewCollection GetAllProductReviews(bool showHidden)
        {
            ProductReviewCollection productReviewCollection = new ProductReviewCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductReviewLoadAll");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    ProductReview productReview = new ProductReview();
                    productReview.ProductReviewID = NopDataHelper.GetInt(dataReader, "ProductReviewID");
                    productReview.ProductID = NopDataHelper.GetInt(dataReader, "ProductID");
                    productReview.CustomerID = NopDataHelper.GetInt(dataReader, "CustomerID");
                    productReview.Title = NopDataHelper.GetString(dataReader, "Title");
                    productReview.ReviewText = NopDataHelper.GetString(dataReader, "ReviewText");
                    productReview.Rating = NopDataHelper.GetInt(dataReader, "Rating");
                    productReview.HelpfulYesTotal = NopDataHelper.GetInt(dataReader, "HelpfulYesTotal");
                    productReview.HelpfulNoTotal = NopDataHelper.GetInt(dataReader, "HelpfulNoTotal");
                    productReview.IsApproved = NopDataHelper.GetBoolean(dataReader, "IsApproved");
                    productReview.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    productReviewCollection.Add(productReview);
                }
            }

            return productReviewCollection;
        }

        public static ProductReview InsertProductReview(int ProductID, int CustomerID, string Title,
            string ReviewText, int Rating, int HelpfulYesTotal,
            int HelpfulNoTotal, bool IsApproved, DateTime CreatedOn)
        {
            ProductReview productReview = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductReviewInsert");
            db.AddOutParameter(dbCommand, "ProductReviewID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, ProductID);
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, CustomerID);
            db.AddInParameter(dbCommand, "Title", DbType.String, Title);
            db.AddInParameter(dbCommand, "ReviewText", DbType.String, ReviewText);
            db.AddInParameter(dbCommand, "Rating", DbType.Int32, Rating);
            db.AddInParameter(dbCommand, "HelpfulYesTotal", DbType.Int32, HelpfulYesTotal);
            db.AddInParameter(dbCommand, "HelpfulNoTotal", DbType.Int32, HelpfulNoTotal);
            db.AddInParameter(dbCommand, "IsApproved", DbType.Boolean, IsApproved);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int ProductReviewID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@ProductReviewID"));
                productReview = GetByProductReviewID(ProductReviewID);
            }
            return productReview;
        }

        public static ProductReview UpdateProductReview(int ProductReviewID, int ProductID, int CustomerID, string Title,
            string ReviewText, int Rating, int HelpfulYesTotal,
            int HelpfulNoTotal, bool IsApproved, DateTime CreatedOn)
        {
            ProductReview productReview = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductReviewUpdate");
            db.AddInParameter(dbCommand, "ProductReviewID", DbType.Int32, ProductReviewID);
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, ProductID);
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, CustomerID);
            db.AddInParameter(dbCommand, "Title", DbType.String, Title);
            db.AddInParameter(dbCommand, "ReviewText", DbType.String, ReviewText);
            db.AddInParameter(dbCommand, "Rating", DbType.Int32, Rating);
            db.AddInParameter(dbCommand, "HelpfulYesTotal", DbType.Int32, HelpfulYesTotal);
            db.AddInParameter(dbCommand, "HelpfulNoTotal", DbType.Int32, HelpfulNoTotal);
            db.AddInParameter(dbCommand, "IsApproved", DbType.Boolean, IsApproved);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);

            if (db.ExecuteNonQuery(dbCommand) > 0)
                productReview = GetByProductReviewID(ProductReviewID);

            return productReview;
        }

        public static void SetProductRatingHelpfulness(int ProductReviewID,
            int CustomerID, bool WasHelpful)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductReviewHelpfulnessCreate");
            db.AddInParameter(dbCommand, "ProductReviewID", DbType.Int32, ProductReviewID);
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, CustomerID);
            db.AddInParameter(dbCommand, "WasHelpful", DbType.Boolean, WasHelpful);
            db.ExecuteNonQuery(dbCommand);
        }
        #endregion
    }
}
